package com.example.remindme.HomePages.userDatabase

import android.util.Log
import androidx.lifecycle.*
import com.example.register_demo.userDatabase.UserDatabase
import com.example.register_demo.userDatabase.UserEntity
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class DatabaseViewModel(application: android.app.Application) :AndroidViewModel(application) {

    var rememberAccount:List<UserEntity>?=null
    var finished= MutableLiveData<Boolean>() // 是否已经记住用户
    var is_remember:Boolean=false
    private val userDatabase by lazy {
        UserDatabase.getDatabase(getApplication())  // 数据库在实例化的时候需要。。Context
    }
    public val userDao by lazy {
        userDatabase.userDao()
    }
    // 让dao对外公开，进行数据访问
    private var userInfo:LiveData<List<UserEntity>>?=null
    fun getRealTimeUser(account:String):LiveData<List<UserEntity>>
    {
        viewModelScope.launch {
            // 使用协程
            userInfo=userDao.getRealTimeUser(account)

        }      //userDatabase=
        return userInfo!!
    }
    suspend fun queryRemember()
    {
        coroutineScope {
           rememberAccount=userDao.getRemember()
            finished.postValue(true)//
        }
    }
  suspend fun isEqual(account: String,password:String):Boolean
    {
        var search: List<UserEntity>?
        var ifEqual:Boolean=false


        coroutineScope{
            search=userDao.getLoginInfo(account)  // 通过viewmodel，实现database，访问dao定义的接口
            if(search!!.isEmpty())
                Log.d("login","there isn't your account")
            else
            {

                search!!.forEach{
                    if(it.password==password)
                    {
                        ifEqual=true
                        // 验证通过后更新数据库
                        if(is_remember)
                        {
                            val temp=UserEntity(it.account,it.password,it.nickname,1)
                            userDao.updateAccount(temp)
                        }
                    }
                }

            }
        }


        return ifEqual
    }

}
/*
class AndroidViewModelFactory private constructor(
    private val mApplication: Application,
    private val str:String
) : ViewModelProvider.NewInstanceFactory() {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return if (AndroidViewModel::class.java.isAssignableFrom(modelClass)) {
            try {
                modelClass.getConstructor(
                    Application::class.java, String::class.java
                ).newInstance(mApplication, str)
            } catch (e: Exception) {
                throw RuntimeException("Cannot create an instance of $modelClass", e)
            }
        } else super.create(modelClass)
    }

    companion object {
        private var sInstance: AndroidViewModelFactory? = null
        fun getInstance(
            application: Application,
            name: String
        ): AndroidViewModelFactory {
            if (sInstance == null) {
                sInstance = AndroidViewModelFactory(application, name)
            }
            return sInstance as AndroidViewModelFactory
        }
    }
}

 */
